package com.example.zengfansheng.notificationpro;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private static final int NOTIFICATION_ID = 200;
    private NotificationManager nm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /***
     * API 16以上的高级通知实现
     *
     * @param v
     */
    public void sendNotification16(View v) {
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());

        //1、大的图片
        NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.mars);
        bigPictureStyle.bigPicture(bitmap);
        bigPictureStyle.setSummaryText("BigPictureStyle Summary Text");
        bigPictureStyle.setBigContentTitle("BigPictureStyle Big Content Title");

        //2、长文本
        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
        bigTextStyle.bigText(
                "bigger than bigger bigger than bigger bigger than bigger bigger than bigger bigger than bigger "
                        + "bigger than bigger bigger than bigger bigger than bigger bigger than bigger bigger than "
                        + "bigger的的顶顶顶顶顶的" +
                        "bigger than bigger bigger than bigger bigger than bigger bigger than bigger bigger than "
                        + "bigger bigger than bigger bigger than bigger bigger than bigger bigger than bigger bigger "
                        + "than bigger的的顶顶顶顶顶的" +
                        "bigger than bigger bigger than bigger bigger than bigger bigger than bigger bigger than "
                        + "bigger bigger than bigger bigger than bigger bigger than bigger bigger than bigger bigger "
                        + "than bigger的的顶顶顶顶顶的");
        bigTextStyle.setBigContentTitle("BigTextStyle BigContentTitle");

        // 3、文字列表集合
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        inboxStyle.setBigContentTitle("InboxStyle BigContentTitle");
        inboxStyle.setSummaryText("InboxStyle SummaryText");
        for (int i = 0; i < 10; i++) {
            inboxStyle.addLine("InboxStyle box" + i);
        }

        // 下拉后的小视图内容
        builder = builder.setSmallIcon(android.R.drawable.ic_menu_camera)
                .setTicker("我是ticker")
                .setContentTitle("Content Title")
                .setContentText("Content Text")
                .setSubText("Sub Text")
                .setWhen(System.currentTimeMillis())
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.mars));
        builder.setStyle(bigPictureStyle);
        //                builder.setStyle(bigTextStyle);
        //        builder.setStyle(inboxStyle);

        // ==================全屏和删除==================
        //        Intent intent = new Intent(this, MainActivity.class);
        //        PendingIntent contentIntent = PendingIntent.getActivity(this, 311, intent, PendingIntent
        // .FLAG_UPDATE_CURRENT);

        // 1.设置一个普通意图
        //        builder.setContentIntent(contentIntent);

        /**
         *  2.设置全屏意图
         *
         * 4.x SDK的效果：任何时候发送的这类型的通知都会直接执行里面的意图(以前要执行该意图，用户必须点击该通知才会执行，这里不需要点击了---变态功能)
         *
         * 5.x SDK的效果：任何时候发送的这类型的通知都会在屏幕上方弹出一个区域提示。
         *
         * 补充：虽然强大，但是一旦执行了一次，下次下拉后再点击动作就不会再执行了。
         */
        //        builder.setFullScreenIntent(contentIntent, true);

        // 3.删除该通知的时候执行的意图。
        //        builder.setDeleteIntent(contentIntent);

        // ====================TaskStackBuilder==============
        // 1）添加ResultActivity中parentActivityName到ResultActivity栈的下面，到时back回退到这里
        TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(getApplicationContext());
        // Adds the back stack(找到MainActivity的android:parentActivityName将其添加到任务栈里面)
        taskStackBuilder.addParentStack(ResultActivity.class);

        // 2）点击Notification要启动的Activity，位于栈顶
        Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
        // Adds the Intent to the top of the stack(将自己添加到任务栈的顶部)
        taskStackBuilder.addNextIntent(intent);

        // Gets a PendingIntent containing the entire back stack
        Bundle options = new Bundle();
        PendingIntent contentIntent =
                taskStackBuilder.getPendingIntent(200, PendingIntent.FLAG_UPDATE_CURRENT, options);
        builder.setContentIntent(contentIntent);

        //        id，当使用FLAG_UPDATE_CURRENT时，id一样的几个通知都会更新。
        nm.notify(NOTIFICATION_ID, builder.build());
        //         区别：tag和id两个都有的时候就一起作为唯一的标示。
        //        nm.notify(tag, NOTIFICATION_ID, builder.build());
    }

}
